import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:xsdk_flutter/xsdk/operation/x_constant_temp.dart';
import 'package:xsdk_flutter/xsdk/utils/http/x_utils_http.dart';
import 'package:xsdk_flutter/xsdk/utils/webserver/support/x_restful_api_router.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_date.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_file.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_jiami.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_response.dart';

/*
RESTFul风格,具有一组相同url前缀的api
使用命令自动生成Router: flutter pub run build_runner build --delete-conflicting-outputs

dev_dependencies:
  build_runner: ^2.4.8
  shelf_router_generator: ^1.1.0

 */
part 'x_demo_restful_api_router_get_photo.g.dart';

class XDemoRESTFulApiRouter_Photo extends XRESTFulApiRouter{

  //缓存图片文件夹路径
  late String cachePhotoPath;
  //缓存图片文件夹名称
  String cachePhotoDirectoryName='cachePhoto';

  int? port;

  XDemoRESTFulApiRouter_Photo(
      {
        required this.port,
      }
      ):super(urlPrefixPath:'/photo') {
    //初始化文件夹
    //XFileUtils.init();已在app中初始化
    cachePhotoPath='${XTempData.appDocumentsDirectoryPath}/$cachePhotoDirectoryName';
    XFileUtils.initFolderPath(cachePhotoPath);

  }

  //自定义路由
  @Route.get('/')
  Future<Response> test1(Request request) async {
    return Response.ok('test!');
  }

  /*
  加载本地缓存图片(不压缩)
  注意:不能用?拼接
  photoUrl 需要对图片url转换处理再传入: XJiamiUtils.encodeUrl()

  访问: http://192.168.8.15:9000/photo/getLocalCachePhoto=图片正式地址
   */
  @Route.get('/getLocalCachePhoto=<photoUrl>')
  Future<Response> getLocalCachePhoto(Request request,String photoUrl) async {
    XLogUtils.printLog('---getLocalCachePhoto,photoUrl=$photoUrl');
    //解密还原图片url
    String url=XJiamiUtils.decodeUrl(photoUrl);
    XLogUtils.printLog('---getLocalCachePhoto,url=$url');
    //截取出图片名
    int index_start=-1;
    int index_end=-1;
    String? photoName;

    //
    bool isSupport=false;
    String? suffixName;
    if(url.contains('.png')){
      isSupport=true;
      suffixName='.png';
    }else if(url.contains('.jpg')
    ){
      isSupport=true;
      suffixName='.jpg';
    }else if(url.contains('.jpeg')){
      isSupport=true;
      suffixName='.jpeg';
    }else if(url.contains('.gif')){
      isSupport=true;
      suffixName='.gif';
    }else if(url.contains('.bmp')){
      isSupport=true;
      suffixName='.bmp';
    }else if(url.contains('.svg')){
      isSupport=true;
      suffixName='.svg';
    }

    if(isSupport){

      index_end=url.indexOf('$suffixName');
      String url1=url.substring(0,index_end);
      index_start=url1.lastIndexOf('/');
      photoName=url.substring(index_start+1,index_end);
      photoName='$photoName$suffixName';

      //判断本地缓存图片是否存在
      bool isFileExistsSync=XFileUtils.isFileExistsSync('$cachePhotoPath/$photoName');
      if(!isFileExistsSync){
        //下载图片
        XLogUtils.printLog('---正在下载图片= $photoName');
        await XHttpUtils.download_sync(url: url, savePath: '$cachePhotoPath/$photoName');
      }else{
        XLogUtils.printLog('---图片已存在= $photoName');
      }

      //重定向到本地图片url
      return Response.seeOther('http://${XTempData.localWifiIP}:$port/$cachePhotoDirectoryName/$photoName');
    }

    return Response.notFound('photoUrl: $photoUrl');
  }

  /*
  加载本地缓存图片(压缩...),
  photoUrl 需要对图片url转换处理再传入: XJiamiUtils.encodeUrl()
   */
  @Route.get('/getLocalCachePhoto=<photoUrl>/thumb/wh500/quality80')
  Future<Response> getLocalCachePhotoThumb(Request request,String photoUrl) async {

    return Response.notFound('photoUrl: $photoUrl');
  }








  //
  @override
  Router get router => _$XDemoRESTFulApiRouter_PhotoRouter(this);

}


